package com.smw.config;


import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;
import com.smw.myrealm.Myrealm1;
import com.smw.myrealm.Myrealm2;
import com.smw.myrealm.Myrealm3;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {
    //添加thymelaf集成shiro的插件
    @Bean
    public ShiroDialect shiroDialect(){
        return new ShiroDialect();
    }
    @Bean
    public ShiroFilterFactoryBean shiroFilterFactoryBean(@Qualifier(value = "manager" )DefaultWebSecurityManager manager){
//测试git 这里是分支master册数
        ShiroFilterFactoryBean bean= new ShiroFilterFactoryBean();
        bean.setSecurityManager(manager);
        Map<String, String> map=new HashMap<String,String>();
        map.put("/user/query","authc");
        map.put("/user/update","authc");
        bean.setFilterChainDefinitionMap(map);
        bean.setLoginUrl("/user/toLogin");
        return bean;
    }
    @Bean(name = "manager")
    public DefaultWebSecurityManager defaultWebSecurityManager(
            @Qualifier(value = "myrealm1") Myrealm1 myrealm,
            @Qualifier(value = "myrealm2") Myrealm2 myrealm2,
            @Qualifier(value = "myrealm3") Myrealm3 myrealm3
    ){
        DefaultWebSecurityManager securityManager=new DefaultWebSecurityManager();



        //装配上Realm
        Collection<Realm> realms = new ArrayList();
        realms.add(myrealm);
        realms.add(myrealm2);
        realms.add(myrealm3);

        securityManager.setRealms(realms);

        return securityManager;
    }
    @Bean(name = "myrealm1")
    public Myrealm1 myRealm1(){
        Myrealm1 myrealm1=new Myrealm1();
        HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
        //设置加密算法为md5
        hashedCredentialsMatcher.setHashAlgorithmName("md5");
        //设置加密算法的散列次数
        hashedCredentialsMatcher.setHashIterations(1024);

        //将自定义匹配添加进Realm中
        myrealm1.setCredentialsMatcher(hashedCredentialsMatcher);


        return myrealm1;
    }
    @Bean(name = "myrealm2")
    public Myrealm2 myRealm2(){
        Myrealm2 myrealm2=new Myrealm2();
        HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
        //设置加密算法为md5
        hashedCredentialsMatcher.setHashAlgorithmName("md5");
        //设置加密算法的散列次数
        hashedCredentialsMatcher.setHashIterations(1024);
        //将自定义匹配添加进Realm中
        myrealm2.setCredentialsMatcher(hashedCredentialsMatcher);
        return myrealm2;
    }
    @Bean(name = "myrealm3")
    public Myrealm3 myRealm3(){
        Myrealm3 myrealm3=new Myrealm3();
        HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
        //设置加密算法为md5
        hashedCredentialsMatcher.setHashAlgorithmName("SHA-256");
        //设置加密算法的散列次数
        hashedCredentialsMatcher.setHashIterations(1024);

        //将自定义匹配添加进Realm中
        myrealm3.setCredentialsMatcher(hashedCredentialsMatcher);
        return myrealm3;
    }
}
